package com.lin.music;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    Button prebtn,stopbtn,startbtn,nextbtn,pausebtn,cyclebtn;
    TextView text;
    private boolean ispaused=false;
    private MediaPlayer mediaPlayer;
    private static final File Music_Path=new File("/sdcard/mp3/");
    public List<String>musiclist;
    public int songnum=0;
    public String songname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer=new MediaPlayer();
        findId();
        listener();


    }
    void findId()
    {
        cyclebtn=(Button)findViewById(R.id.cycle);
        prebtn=(Button)findViewById(R.id.pre);
        stopbtn=(Button)findViewById(R.id.stop);
        startbtn=(Button)findViewById(R.id.start);
        nextbtn=(Button)findViewById(R.id.next);
        pausebtn=(Button)findViewById(R.id.pause);
        text=(TextView)findViewById(R.id.text);

    }
    void listener()
    {
        cyclebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cycle();
            }
        });
        prebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pre();
            }
        });
        stopbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if(mediaPlayer!=null)
                    {
                        text.setText("stop");
                        mediaPlayer.stop();
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        });

        startbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                start();
            }

        });

        nextbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                next();
            }
        });
        pausebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    if(mediaPlayer!=null)
                    {
                        if(ispaused==false)
                        {
                            mediaPlayer.pause();
                            ispaused=true;
                            text.setText("pause");
                        }
                        else if(ispaused==true)
                        {
                            mediaPlayer.start();
                            ispaused=false;
                            text.setText("restart");
                        }
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                try {
                    mediaPlayer.release();
                    text.setText("release");

                }catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                try{
                    mediaPlayer.release();
                    text.setText("error");

                }catch (Exception e)
                {
                    e.printStackTrace();
                }
                return false;
            }
        });

    }
    public void cycle()
    {
      cyclestart();
    }
    public void cyclestart()
    {
        mediaPlayer.reset();
        try {
            mediaPlayer.reset();
            String dataSource=musiclist.get(songnum);
            setsongname(dataSource);
            mediaPlayer.setDataSource(dataSource);
            mediaPlayer.prepare();
            mediaPlayer.start();
            text.setText("start");

        }catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalStateException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                cycle();
            }
        });

    }
    public MainActivity()
    {
        musiclist=new ArrayList<String>();
        if(Music_Path.listFiles(new MusicFilter()).length>0)
        {
            for(File file:Music_Path.listFiles(new MusicFilter()))
            {
                musiclist.add(file.getAbsolutePath());
            }
        }
    }
    public void setsongname(String dataSource)
    {
        File file=new File(dataSource);
        String name=file.getName();
        int index=name.lastIndexOf(".");
        songname=name.substring(0,index);
    }
    public void start()
    {
        mediaPlayer.reset();
        try {
            mediaPlayer.reset();
            String dataSource=musiclist.get(songnum);
            setsongname(dataSource);
            mediaPlayer.setDataSource(dataSource);
            mediaPlayer.prepare();
            mediaPlayer.start();
            text.setText("start");

        }catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalStateException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                next();
            }
        });
    }
    public void next()
    {
        if(songnum==musiclist.size()-1)
            songnum=0;
        else
            songnum++;
        start();

    }
    public void pre()
    {
        if(songnum==0)
            songnum=musiclist.size()-1;
        else
            songnum=songnum-1;
        start();
    }

}